package village.rainbow.dailyscene.database;

import java.io.InputStream;
import java.util.Hashtable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import village.rainbow.dailyscene.Global;
import android.content.Context;

public class QueryXmlManager {

	private Context _context;
	private Hashtable<String, String> _select = new Hashtable<String, String>();
	private Hashtable<String, String> _update = new Hashtable<String, String>();
	private Hashtable<String, String> _delete = new Hashtable<String, String>();
	private Hashtable<String, String> _insert = new Hashtable<String, String>();

	public QueryXmlManager(Context context, String xmlFileName) throws Exception {
		_context = context;
		init(xmlFileName);
	}

	private void init(String xmlFileName) throws Exception {
		Document doc = null;
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		InputStream iStream = _context.getAssets().open(Global.queryFilePath + xmlFileName);
		doc = builder.parse(iStream);

		NodeList selectList = doc.getElementsByTagName("select");
		NodeList updateList = doc.getElementsByTagName("update");
		NodeList deleteList = doc.getElementsByTagName("delete");
		NodeList insertList = doc.getElementsByTagName("insert");

		NodeList includeList = doc.getElementsByTagName("include");
		addEmbededXmlFile(includeList);
		
		addItemFromXml(_select, selectList);
		addItemFromXml(_update, updateList);
		addItemFromXml(_delete, deleteList);
		addItemFromXml(_insert, insertList);
	}
	
	private void addEmbededXmlFile(NodeList includeList) throws Exception {
		for(int i=0; i<includeList.getLength(); i++){
			Node node = includeList.item(i);
			Node value = node.getAttributes().getNamedItem("value");
			String nodeValue = value.getNodeValue();
			if(nodeValue.indexOf("xml") == -1)
				continue;
			init(nodeValue);
		}
	}
	
	private void addItemFromXml(Hashtable<String, String> table, NodeList nodelist){
		for(int i=0; i<nodelist.getLength(); i++){
			Node node = nodelist.item(i);
			Node idNode = node.getAttributes().getNamedItem("id");
			table.put(idNode.getNodeValue(), node.getTextContent());
		}
		
	}

	public String getSelect(String id) throws Exception {
		if(!_select.containsKey(id)) {
			throw new Exception("찾을 수 없는 select id 입니다.");
		}
		return _select.get(id);
	}

	public String getUpdate(String id) throws Exception {
		if(!_update.containsKey(id)) {
			throw new Exception("찾을 수 없는 update id 입니다.");
		}
		return _update.get(id);
	}

	public String getDelete(String id) throws Exception {
		if(!_delete.containsKey(id)) {
			throw new Exception("찾을 수 없는 delete id 입니다.");
		}
		return _delete.get(id);
	}

	public String getInsert(String id) throws Exception {
		if(!_insert.containsKey(id)) {
			throw new Exception("찾을 수 없는 insert id 입니다.");
		}
		return _insert.get(id);
	}



}
